package com.mareas.web.jsf.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.springframework.stereotype.Component;
import org.springframework.web.jsf.FacesContextUtils;

import com.mareas.web.model.Datosarribo;
import com.mareas.web.model.Operacion;
import com.mareas.web.model.Proforma;
import com.mareas.web.model.VariableDeProforma;

@Component("addProformaView")
@ManagedBean
@RequestScoped
public class AddProformaView extends ListAbstractView<Proforma> implements
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1993275622630350022L;
	/**
	 *
	 */
	private List<VariableDeProforma> variables = new ArrayList<VariableDeProforma>();
	private List<VariableDeProforma> variablesSelected = new ArrayList<VariableDeProforma>();
	private List<String> variablesIdSelected = new ArrayList<String>();
	private String deletedVariableId;
	private Operacion operacionActual = null;

	public AddProformaView() {
		super();

	}

	@Override
	public void store() {
		getEdited().setVariableDeProformas(
				new HashSet<VariableDeProforma>(variablesSelected));
		super.store();
	}

	@Override
	public List<Proforma> getAll() {
		List<Proforma> all = this.getGenericService().getAll(Proforma.class);
		return all;
	}

	@Override
	public Proforma getEdited() {
		if (super.getEdited() == null) {
			if (getOperacionActual() != null
					&& !getOperacionActual().getProformas().isEmpty()) {

				for (Proforma proforma : operacionActual.getProformas()) {
					setEdited(proforma);
					setVariablesSelected(new ArrayList<VariableDeProforma>((proforma.getVariableDeProformas())));
				}
			} else {
				super.setEdited(new Proforma());
			}
			
		}
		return super.getEdited();
	}

	@Override
	public void setEdited(Proforma edited) {
		super.setEdited(edited);
	}

	@Override
	public Proforma getNew() {
		return new Proforma();
	}

	public void resetEdited() {
		setEdited(getNew());
	}

	
	public List<String> getVariablesIdSelected() {
		return variablesIdSelected;
	}

	public void setVariablesIdSelected(List<String> variablesIdSelected) {
		this.variablesIdSelected = variablesIdSelected;
	}

	public void saveVariables() {
		VariableDeProforma var = null;
		for (String id : variablesIdSelected) {
			var = getVariableById(id);
			if (!getVariablesSelected().contains(var))
				getVariablesSelected().add(var);
		}		
	}
	
	private VariableDeProforma getVariableById(String id) {
		for (VariableDeProforma var : getVariables()) {
			if(var.getVariableId().toString().equals(id)) {
				return var;
			}
		}
		return null;
	}

	public List<VariableDeProforma> getVariables() {
		if (variables.isEmpty()) {
			variables = getGenericService().getAll(VariableDeProforma.class);
		}

		return variables;
	}

	public void setVariables(List<VariableDeProforma> variables) {
		this.variables = variables;
	}

	public List<VariableDeProforma> getVariablesSelected() {
		return variablesSelected;
	}

	public void setVariablesSelected(List<VariableDeProforma> variablesSelected) {
		this.variablesSelected = variablesSelected;
	}

	public String getDeletedVariableId() {
		return deletedVariableId;
	}

	public void setDeletedVariableId(String deletedVariableId) {
		this.deletedVariableId = deletedVariableId;
	}

	public void removeVariable() {
		variablesSelected.remove(getVariableById(deletedVariableId));
		variablesIdSelected.remove(deletedVariableId);
	}

	public void init() {
		//super.init();
		operacionActual = null;
//		variables = new ArrayList<VariableDeProforma>();
//		variablesSelected = new ArrayList<VariableDeProforma>();
//		variablesIdSelected = new ArrayList<String>();
//		deletedVariableId = null;
	}
	
	public Operacion getOperacionActual() {

		if (this.operacionActual == null) {
			FacesContext facescontext = FacesContext.getCurrentInstance();
			ApplicationBean applicationBean = (ApplicationBean) FacesContextUtils
					.getRequiredWebApplicationContext(facescontext).getBean(
							"applicationBean");
			this.operacionActual = applicationBean.getOperacionActual();
			
			if (this.operacionActual != null) {
				this.operacionActual = getGenericService().get(Operacion.class, operacionActual.getOperacionId());
			}
		}
		return this.operacionActual;
	}	
}
